package com.kejiguan.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kejiguan.ui.R;
import com.kejiguan.ui.model.DisplayArea;
import com.kejiguan.ui.model.MenuItem;

import java.util.List;

/**
 * Created by YYY on 2016/5/1.
 */
public class HomeMenuAdapter extends RecyclerView.Adapter<HomeMenuVH> {

    private Context context;
    private List<MenuItem> menuItems;
    private LayoutInflater inflater;
    private MenuItem selectMenu;
    private View.OnClickListener onClickListener;

    private int selectColor,unSelectColor;
    private int selectBgColor,unSelectBgColor;

    public HomeMenuAdapter(Context context, List<MenuItem> menuItems) {
        this.context = context;
        this.menuItems = menuItems;
        inflater = LayoutInflater.from(context);
        if (menuItems!=null&&menuItems.size()>0)
        {
            selectMenu=menuItems.get(0);
        }

        selectColor=context.getResources().getColor(R.color.text_color2);
        unSelectColor=context.getResources().getColor(R.color.text_color1);
        selectBgColor=context.getResources().getColor(R.color.text_color4);
        unSelectBgColor=context.getResources().getColor(R.color.text_color5);
    }


    @Override
    public HomeMenuVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.home_menu_list_item, null);
        return new HomeMenuVH(itemView);
    }

    @Override
    public void onBindViewHolder(HomeMenuVH holder, int position) {

        MenuItem menuItem=menuItems.get(position);
        holder.menuName.setText(menuItem.getLabel());

        if (menuItem.getMenuId()==selectMenu.getMenuId())
        {
            holder.menuName.setBackgroundColor(selectBgColor);
            holder.menuName.setTextColor(selectColor);
        }else
        {
            holder.menuName.setBackgroundColor(unSelectBgColor);
            holder.menuName.setTextColor(unSelectColor);
        }

        if (null!=onClickListener)
        {
            holder.itemView.setTag(R.id.recyclerview_item_position, position);
            holder.itemView.setOnClickListener(onClickListener);
        }


    }

    @Override
    public int getItemCount() {
        if (null == menuItems)
            return 0;
        else
            return menuItems.size();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setSelectMenu(int position)
    {
        selectMenu=menuItems.get(position);
        notifyDataSetChanged();
    }

    public MenuItem getSelectArea() {
        return selectMenu;
    }

    public MenuItem getMenu(int position) {
        return menuItems.get(position);
    }


    public boolean setDisplaySelected()
    {

        for (int i=0;i<menuItems.size();i++)
        {
            if (menuItems.get(i).getMenuId()==202)
            {
                selectMenu=menuItems.get(i);
                notifyDataSetChanged();
                return true;
            }
        }

        return false;
    }
}
